package cn.tedu.tmall.front.mall.dao.persist.repository.impl;

import cn.tedu.tmall.common.pojo.vo.PageData;
import cn.tedu.tmall.common.util.PageInfoToPageDataConverter;
import cn.tedu.tmall.front.mall.dao.persist.mapper.CommentMapper;
import cn.tedu.tmall.front.mall.dao.persist.repository.ICommentRepository;
import cn.tedu.tmall.front.mall.pojo.entity.Comment;
import cn.tedu.tmall.front.mall.pojo.vo.CommentListItemVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 处理商品评论数据的存储库实现类
 *
 * @author java@tedu.cn
 * @version 3.0
 */
@Slf4j
@Repository
public class CommentRepositoryImpl implements ICommentRepository {

    @Autowired
    private CommentMapper commentMapper;

    public CommentRepositoryImpl() {
        log.info("创建存储库对象：CommentRepositoryImpl");
    }

    @Override
    public int insert(Comment comment) {
        log.debug("开始执行【插入评论】的数据访问，参数：{}", comment);
        return commentMapper.insert(comment);
    }

    @Override
    public PageData<CommentListItemVO> list(Long goodsId, Integer pageNum, Integer pageSize) {
        log.debug("开始执行【根据商品查询评论数据列表】的数据访问，商品ID：{}，页码：{}，每页记录数：{}", pageNum, pageSize);
        PageHelper.startPage(pageNum, pageSize);
        List<CommentListItemVO> list = commentMapper.list(goodsId);
        PageInfo<CommentListItemVO> pageInfo = new PageInfo<>(list);
        return PageInfoToPageDataConverter.convert(pageInfo);
    }

}
